package com.ruoyi.bank.controller;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.domain.BankUser;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.BankUser;
import com.ruoyi.bank.service.IBankUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 小程序用户Controller
 * 
 * @author ruoyi
 * @date 2022-06-15
 */
@RestController
@RequestMapping("/bankuser")
public class BankUserController extends BaseController
{
    @Autowired
    private IBankUserService bankUserService;

    /**
     * 查询小程序用户列表
     */
    @RequiresPermissions("bank:bankuser:list")
    @GetMapping("/list")
    public TableDataInfo list(BankUser bankUser)
    {
        startPage();
        List<BankUser> list = bankUserService.selectBankUserList(bankUser);
        return getDataTable(list);
    }

    /**
     * 导出小程序用户列表
     */
    @RequiresPermissions("bank:bankuser:export")
    @Log(title = "小程序用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BankUser bankUser)
    {
        List<BankUser> list = bankUserService.selectBankUserList(bankUser);
        ExcelUtil<BankUser> util = new ExcelUtil<BankUser>(BankUser.class);
        util.exportExcel(response, list, "小程序用户数据");
    }

    /**
     * 获取小程序用户详细信息
     */
    @RequiresPermissions("bank:bankuser:query")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(bankUserService.selectBankUserByUserId(userId));
    }

    /**
     * 新增小程序用户
     */
    @RequiresPermissions("bank:bankuser:add")
    @Log(title = "小程序用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BankUser bankUser)
    {
        return toAjax(bankUserService.insertBankUser(bankUser));
    }

    /**
     * 修改小程序用户
     */
    @RequiresPermissions("bank:bankuser:edit")
    @Log(title = "小程序用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BankUser bankUser)
    {
        return toAjax(bankUserService.updateBankUser(bankUser));
    }

    /**
     * 删除小程序用户
     */
    @RequiresPermissions("bank:bankuser:remove")
    @Log(title = "小程序用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(bankUserService.deleteBankUserByUserIds(userIds));
    }

    /**
     * 获取当前用户信息
     */
    @InnerAuth
    @GetMapping("/info/{openId}")
    public R<LoginUser> info(@PathVariable("openId") String openId)
    {
        BankUser user = new BankUser();
        user.setOpenId(openId);
        List<BankUser> userList = bankUserService.selectBankUserList(user);
        if (userList.size() == 0)
        {
            return R.ok(null);
        }else{
            LoginUser sysUserVo = new LoginUser();
            SysUser sysUser = new SysUser();
            sysUser.setUserId(userList.get(0).getUserId());
            sysUser.setUserName(userList.get(0).getUserId().toString());
            sysUserVo.setSysUser(sysUser);
            return R.ok(sysUserVo);
        }


    }

    /**
     * 注册用户信息
     */
    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody BankUser user)
    {
        return R.ok(1==bankUserService.insertBankUser(user));
    }
}
